package com.zyl.ag.timer;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.zyl.ag.bean.AgTD;
import com.zyl.ag.ui.widget.WidgetProvider;

import com.zyl.util.FileUtil;
import com.zyl.util.SharedPreferencesUtil;
import com.zyl.util.UrlUtil;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

public class TimerService extends Service {

	

	@Override
	public IBinder onBind(Intent intent) {

		return null;

	}

	@Override
	public void onCreate() {
		super.onCreate();

	}

	@Override
	public void onDestroy() {

	}

	@Override
	public void onStart(Intent intent, int startid) {
	
			// alert(poll() );
			 String str=UrlUtil.getAgTD();
			 AgTD ag=AgTD.getAgTD(str);  
			 compare(intent,ag);
			 
			 String text= ag.toString();
			  
	    	 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(TimerService.this);
	         WidgetProvider.updateTextAppWidget(TimerService.this, appWidgetManager, text);
	    	         
	}

	private void compare(Intent intent,AgTD ag) {
		 
		try {
			
		int price=	(int)Float.parseFloat(ag.getPrice());
		
		
		int h =SharedPreferencesUtil.getHigh(TimerService.this);	
		int l =SharedPreferencesUtil.getLow(TimerService.this);	
		
			if(h>0&&price>=h){
				alert("当前价格"+price+"已经大于设定止盈价格"+h+"请止盈");
				vibrator();
			}
			if(l>0&&l>=price){
				alert("当前价格"+price+"已经小于设定止损价格"+l+"请止损");
				vibrator();
			}
			 
		} catch (Exception e) {
		 e.printStackTrace();
		}
	}

	




	public void vibrator() {
		Intent i = new Intent(this, TimerVibrator.class);
		PendingIntent pi = PendingIntent.getBroadcast(
				this.getApplicationContext(), 0, i, 0);
		AlarmManager am = (AlarmManager) this.getSystemService(ALARM_SERVICE);
		am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, pi);

	}
	
	
	
//	private String poll() {
//
//		String str = "";
//		HttpURLConnection conn = null;
//		String loginUri = "http://hq.sinajs.cn/list=SGE_AGTD";
//		try {
//			URL url = new URL(loginUri);
//
//			conn = (HttpURLConnection) url.openConnection();
//			conn.setInstanceFollowRedirects(true);
//			conn.setConnectTimeout(3000);
//			conn.setRequestProperty("Charset", "GBK");
//			conn.connect();
//			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK
//					&& conn.getContentLength() > 0) {
//
//				InputStream localInputStream = conn.getInputStream();
//				BufferedInputStream bufferedInputStream = new BufferedInputStream(
//						localInputStream);
//				InputStreamReader inputStreamReader = new InputStreamReader(
//						bufferedInputStream, "GBK");
//
//				BufferedReader reader = new BufferedReader(inputStreamReader);
//
//				str = reader.readLine();
//				reader.close();
//				bufferedInputStream.close();
//				inputStreamReader.close();
//				bufferedInputStream.close();
//				localInputStream.close();
//			}
//		} catch (MalformedURLException e) {
//
//			e.printStackTrace();
//		} catch (UnsupportedEncodingException e) {
//
//			e.printStackTrace();
//		} catch (IOException e) {
//
//			e.printStackTrace();
//		}
//		return str;
//	}

		private void alert(String string ) {
			// 启动alertDialog源码:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(string)
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									 
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									 
								}
							});
			AlertDialog ad = builder.create();  
			ad.getWindow().setType(
					WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
			ad.getWindow()
					.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
			ad.setCanceledOnTouchOutside(false);
			ad.show();
		}


}
